import axios from "axios";
import { EMessageType } from "../../utils";
export const BASEURL = "https://cn.bing.com";
export const convertBaseUrl = (api: string): string => {
  return `${BASEURL}/${api}`;
};
/**
 * 获取关键词相关的列表
 * @param keyword 关键词
 * @returns 列表
 */
export const sendKeywordRequest = async (
  keyword: string
): Promise<HTMLElement | undefined> => {
  const result = await axios.get<string, { data: HTMLElement | undefined }>(
    convertBaseUrl(EMessageType.Keyword),
    {
      params: {
        pt: "page.chrometab",
        mkt: "zh-cn",
        cp: keyword.length,
        msbqf: false,
        qry: keyword,
        cvid: Math.ceil(Math.random() * 999999999) + "",
      },
    }
  );
  return result.data;
};
/**
 * 获取请求搜索的URL
 * @param keyword
 * @returns
 */
export const sendSearchRequest = (keyword: string): string => {
  return `${convertBaseUrl(EMessageType.Search)}?q=${keyword}&form=CHRDEF`;
};
/**
 * 删除此条历史记录
 * @param keyword 关键词
 */
export const sendRemoveRequest = async (
  keyword: string
): Promise<{ success: boolean }> => {
  const result = await axios.get<
    string,
    {
      data: {
        success: boolean;
      };
    }
  >(convertBaseUrl(EMessageType.RemoveHistory), {
    params: {
      oma: "delete_matching_queries",
      qt: keyword,
      response: "json",
      sig: "3041ABFAA49D45D5A88A82C3EBE0B92B",
      FORM: "ASRMHS",
    },
  });
  return result.data;
};
